"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : 在被踢出集群的节点执行dsscmd工具显示卷组和磁盘使用信息
Create At   : 2023/4/11
Owner       : opentestcase019
Description :
    1.将备机踢出集群
    2.在被踢出集群的节点执行dsscmd工具显示卷组和磁盘使用信息
    3.将备机加入集群
    4.显示卷组和磁盘使用信息
Expect      :
    1.踢出成功
    2.合理报错Failed to get uds connection.
    3.加入成功
    4.显示成功
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.standby_node = Node('Standby1DbUser')
        self.standby_sh = CommonSH('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:将备机踢出集群;expect:踢出成功-----"
        self.logger.info(step1)
        stop_msg = self.standby_sh.exec_cm_ctl(mode='stop',
                                               param='-n 2 -I 6002')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_resource_success_msg,
                      stop_msg, '执行失败' + step1)
        time.sleep(5)
        status = self.standby_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('6002     S Down    Manually stopped', status, '执行失败')

        step2 = "-----step2:在被踢出集群的节点执行dsscmd工具显示卷组和磁盘使用信息;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:将备机加入集群;expect:加入成功-----"
        self.logger.info(step3)
        start_msg = self.standby_sh.exec_cm_ctl(mode='start',
                                                param='-n 2 -I 6002')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_resource_success_msg,
                      start_msg, '执行失败' + step3)
        time.sleep(30)
        status = self.standby_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('6002     S Standby Normal', status, '执行失败' + step3)

        step4 = "-----step4:显示卷组和磁盘使用信息;expect:显示成功-----"
        self.logger.info(step4)
        cmd_list = ['-m B', '-m G -t t', f'-m M -U {macro.UDS_PATH}',
                    f'-t t -U {macro.UDS_PATH}']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.standby_node,
                                                    execute_cmd)
            self.assertIn("Succeed to display lsvg info.",
                          execute_msg, "执行失败" + step4)

    def tearDown(self):
        self.logger.info("-----恢复清理-----")
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        if "6002     S Down    Manually stopped" in status:
            start_msg = self.standby_sh.exec_cm_ctl(mode='start',
                                                    param='-n 2 -I 6002')
            self.logger.info(start_msg)
            self.assertIn(self.constant.cm_start_resource_success_msg,
                          start_msg, '将备机加入集群失败')
        time.sleep(30)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('6002     S Standby Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
